package com.schoolgard.action.alarm;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import net.sf.json.JSONObject;

import com.schoolegard.services.AlarmClassService;
import com.schoolgard.base.BaseAction;
import com.schoolgard.entitybean.AlarmClass;
import com.schoolgard.entitybean.GridDataModel;

public class AlarmClassAction extends BaseAction{

	private AlarmClassService alarmClassService;
	
	private AlarmClass alarmClass;
	
	public void findAll(){
		String startStr = request.getParameter("start"); //grid传过来的当前数据条数
		 String limitStr = request.getParameter("limit"); //每页显示的数据数
		 
		 int start = Integer.parseInt(startStr);
	     int limit = Integer.parseInt(limitStr);
	     if (limit == 0) {
	    	 limit = Integer.MAX_VALUE;
	     }
	     int end = start + limit;  //当前页需要显示的数据 start--> end
		List<AlarmClass> alarms = alarmClassService.findAll(start,limit);
		int total = alarmClassService.findCount();
		if(end > total)end = total;
		
		GridDataModel<AlarmClass> page = new GridDataModel<AlarmClass>();
		page.setPage(start/limit + 1);
		page.setRows(alarms);
		page.setTotal(total);
		String jsonStr = JSONObject.fromObject(page).toString();
		writeToResponse(jsonStr);
	}

	public void save(){
		Long id = alarmClass.getId();
		AlarmClass alarm = null;
		if(id == null || id == 0) {
			alarm = new AlarmClass(); //新增
		} else {
			alarm = alarmClassService.findAlarmClass(id);
		}
		alarm.setEnable(alarmClass.getEnable());
		alarm.setFormat(alarmClass.getFormat());
		alarm.setLevel(alarmClass.getLevel());
		alarm.setName(alarmClass.getName());
		alarm.setType(alarmClass.getType());
		alarmClassService.saveOrUpdate(alarm);
	}
	
	public void delete() {
		String id = request.getParameter("deleteId");
		if(StringUtils.isBlank(id)){
			writeToResponse("0");
			return ;
		}
		alarmClassService.delete(Long.valueOf(id));
		writeToResponse("1");
	}
	
	public AlarmClassService getAlarmClassService() {
		return alarmClassService;
	}

	public void setAlarmClassService(AlarmClassService alarmClassService) {
		this.alarmClassService = alarmClassService;
	}

	public AlarmClass getAlarmClass() {
		return alarmClass;
	}

	public void setAlarmClass(AlarmClass alarmClass) {
		this.alarmClass = alarmClass;
	}
}
